//
//  EditContactViewController.swift
//  HandOffTest
//
//  Created by Lyy on 15/8/19.
//  Copyright (c) 2015年 Lyy. All rights reserved.
//

import UIKit

protocol EditContactViewControllerDelegate {
    func contactWasSaved(contact: Contact)
}

class EditContactViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var txtFirstName: UITextField!
    @IBOutlet weak var txtLastName: UITextField!
    @IBOutlet weak var txtPhoneNumber: UITextField!
    @IBOutlet weak var txtEmail: UITextField!
    
    var delegate: EditContactViewControllerDelegate?
    
    let activityTypeEditContact = "com.yy.handofftest.edit-contact"
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        if let userInfo = userActivity?.userInfo {
            var contact = Contact()
            contact.getContactDataFromDictionary(userInfo)
            
            txtFirstName.text = contact.firstname
            txtLastName.text = contact.lastname
            txtPhoneNumber.text = contact.phoneNumber
            txtEmail.text = contact.email
            
        }
        createUserActivity()
    }
    
    @IBAction func saveContact(sender: AnyObject) {
        var editedContact = Contact()
        
        editedContact.firstname = txtFirstName.text
        editedContact.lastname = txtLastName.text
        editedContact.phoneNumber = txtPhoneNumber.text
        editedContact.email = txtEmail.text
        
        self.delegate?.contactWasSaved(editedContact)
        
        self.performSegueWithIdentifier("idUnwindSegueEditContact", sender: self)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func createUserActivity() {
        userActivity = NSUserActivity(activityType: activityTypeEditContact)
        userActivity?.title = "Edit Contact"
        userActivity?.becomeCurrent()
    }
    
    override func restoreUserActivityState(activity: NSUserActivity) {
        userActivity = activity
        super.restoreUserActivityState(activity)
    }
    
    override func updateUserActivityState(activity: NSUserActivity) {
        if activity == activityTypeEditContact {
            let contact = Contact()
            contact.firstname = txtFirstName.text
            contact.lastname = txtLastName.text
            contact.phoneNumber = txtPhoneNumber.text
            contact.email = txtEmail.text
            
            activity.addUserInfoEntriesFromDictionary(contact.getDictionaryFromContactData() as [NSObject : AnyObject])
            
            super.updateUserActivityState(activity)
        }
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        
        userActivity?.needsSave = true
        
        return true
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "idUnwindSegueEditContact" {
            self.userActivity?.invalidate()
        }
    }
    
}
